/*
 * Desc: 作用域允许你复用通用的逻辑，这种共享逻辑需要定义为类型func(*gorm.DB) *gorm.DB
 * Package: DB
 * File Created: Friday, 16th April 2021 3:33:12 pm
 * Author: rysa (i@rysa.cn)
 * -----
 * Last Modified: Friday, 16th April 2021 3:33:13 pm
 * Modified By: rysa (i@rysa.cn>)
 */

package DB

import "gorm.io/gorm"

type DBCondition func(db *gorm.DB) *gorm.DB

func Paginate(page, pageSize int) DBCondition {
	return func(db *gorm.DB) *gorm.DB {
		return db.Offset((page - 1) * pageSize).Limit(pageSize)
	}
}

func GetTitleLikeCondition(title string) DBCondition {
	return func(tx *gorm.DB) *gorm.DB {
		return tx.Where("title like ?", "%"+title+"%")
	}
}

func GetNameLikeCondition(name string) DBCondition {
	return func(tx *gorm.DB) *gorm.DB {
		return tx.Where("name like ?", "%"+name+"%")
	}
}

func GetOrder(order string) DBCondition {
	return func(tx *gorm.DB) *gorm.DB {
		return tx.Order(order)
	}
}
